package task

import ext.log
import manager.*
import org.json.JSONObject

class StatusWith3UploadTask : Runnable {
    companion object {
        private var count = 0
        private var portCount = 0
    }

    override fun run() {
        try {
            if (count == 0) {
                exec(888f)
            } else if (portCount != StatusManager.StatusCount) {
                exec(StatusManager.status.tempValueFloat)
            } else {
                exec(666f)
            }
            count++
            portCount = StatusManager.StatusCount
        } catch (e: Exception) {
            e.printStackTrace()
        }
        TaskManager.DelayHandler.postDelayed(this, 180 * 1000)
    }

    private fun exec(value: Float) {
        val json = JSONObject()
        json.put("macAddr", AppManager.MacAddress)
        json.put("houseTemperature", value)
        json.put("lowVersion", "1.0")
        json.put("clientVersion", AppManager.Version)
        json.put("signStrength", StatusManager.Signal)
        json.put("cpu", CpuManager.cpuInfo)
        json.put("ram", CpuManager.memoryInfo)
        val content = json.toString()
        log("--3分钟状态----$content")
        val ret = HttpRequestManager.uploadStatusWith3(content)
        log("--3-----$ret")
    }
}